package ipebg.mzk.common.util;

import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class C3P0DatasourcePropertiesFactory {
	
	private static final String PROP_PASSWORD = "password";
	private final static Logger logger = LoggerFactory.getLogger(C3P0DatasourcePropertiesFactory.class);

	public static Properties getProperties(String pwd) throws Exception {
		Properties p = new Properties();
		try {
			p.setProperty(PROP_PASSWORD, decrypt(pwd));//此处加解密算法,可根据项目需要,自行替换
		}// 解密操作
		catch (Exception e) {
			logger.error("C3P0DatasourcePropertiesFactory getProperties is error!", e);
			throw e;
		}
		return p;
	}

	/**
	 * 解密
	 * 
	 * @param ssoToken
	 *            字符串
	 * @return String 返回加密字符串
	 */
	public static String decrypt(String ssoToken) {
		try {
			String name = new String();
			java.util.StringTokenizer st = new java.util.StringTokenizer(ssoToken, "%");
			while (st.hasMoreElements()) {
				int asc = Integer.parseInt((String) st.nextElement()) - 27;
				name = name + (char) asc;
			}

			return name;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 用户名加密
	 * 
	 * @param ssoToken
	 *            字符串
	 * @return String 返回加密字符串
	 */
	public static String encrypt(String ssoToken) {
		try {
			byte[] _ssoToken = ssoToken.getBytes("UTF-8");
			String name = new String();
			// char[] _ssoToken = ssoToken.toCharArray();
			for (int i = 0; i < _ssoToken.length; i++) {
				int asc = _ssoToken[i];
				_ssoToken[i] = (byte) (asc + 27);
				name = name + (asc + 27) + "%";
			}
			return name;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) {
		String temp = "mange.2015";
		System.out.println(temp);
		temp = encrypt(temp);
		System.out.println(temp);
		temp = decrypt(temp);
		System.out.println(temp);
	}
}
